package com.sinoglobal.dumping.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sinoglobal.dumping.R;
import com.squareup.picasso.Picasso;


/**
 * Created by lxy on 2015/10/31.
 */
public class Dumpling_GetMoneyDialog extends Dialog {

    private Context mContext;

    private LayoutInflater mInflater;

    private ImageView imgIcon;

    private TextView tvTitle;

    private TextView tvMoney;           // 钱

    private TextView tvBlessing;        // 祝福

    private TextView tvGreetingCard;    // 贺卡

    private ImageView imgCouponSmall;   // 优惠券-小图

    private ImageView imgCouponBig;     // 优惠券-大图 

    private TextView tvContent;

    private TextView tvToast;

    private TextView tvBtnLeft;

    private TextView tvBtnRight;

    private TextView tvClose;

    private int btnLeftVISIBLE;

    private int btnRightVISIBLE;

    private String strImgUrl;

    private String strTitle;

    private String strCoreField;

    private String strContent;

    private String strToast;

    private String strBtnLeftText;

    private String strBtnRightText;

    private GetMoneyOnSureClick mClick;

    private Toast toast;

    private int mType;

    private int couponSize;

    private ImageView ivDumpling;

    /**
     * dialog类型为钱
     */
    public static final int TYPE_MONEY = 0x1;

    /**
     * dialog类型为祝福
     */
    public static final int TYPE_BLESSING = 0x2;

    /**
     * dialog类型为贺卡
     */
    public static final int TYPE_GREETINGCARD = 0x6;

    /**
     * dialog类型为优惠券
     */
    public static final int TYPE_COUPON = 0x3;

    /**
     * 优惠券种类-小图
     */
    public static final int COUPONSIZE_SMALL = 0x4;

    /**
     * 优惠券种类-大图
     */
    public static final int COUPONSIZE_BIG = 0x5;

    public interface GetMoneyOnSureClick {

        void onCloseClick();

        void onBtnLeftClick();

        void onBtnRightClick();

    }

    private Dumpling_GetMoneyDialog(Context context, Builder builder) {
        super(context, R.style.dumpling_get_money_dialog);

        mContext = context;
        mInflater = LayoutInflater.from(context);
        setCanceledOnTouchOutside(false);

        this.strImgUrl = builder.strImgUrl;
        this.strTitle = builder.strTitle;
        this.strCoreField = builder.strCoreField;
        this.strContent = builder.strContent;
        this.strToast = builder.strToast;
        this.strBtnLeftText = builder.strBtnLeftText;
        this.strBtnRightText = builder.strBtnRightText;
        this.mClick = builder.mClick;
        this.btnLeftVISIBLE = builder.btnLeftVISIBLE;
        this.btnRightVISIBLE = builder.btnRightVISIBLE;
        this.mType = builder.mType;
        this.couponSize = builder.couponSize;
        this.ivDumpling = builder.imageView;

        View view = mInflater.inflate(R.layout.dumpling_dialog_money, null);
        setContentView(view);
        initView(view);
        initClick();
        initData();
        initType();

    }

    private void initView(View view) {

        imgIcon = (ImageView) view.findViewById(R.id.dialog_money_img_icon);
        imgIcon.setVisibility(View.VISIBLE);
        tvTitle = (TextView) view.findViewById(R.id.dialog_money_tv_title);
        tvMoney = (TextView) view.findViewById(R.id.dialog_money_tv_money);
        tvBlessing = (TextView) view.findViewById(R.id.dialog_money_tv_blessing);
        tvGreetingCard = (TextView) view.findViewById(R.id.dialog_money_tv_greetingcard);
        imgCouponSmall = (ImageView) view.findViewById(R.id.dialog_money_img_coupon_small);
        imgCouponBig = (ImageView) view.findViewById(R.id.dialog_money_img_coupon_big);
        tvContent = (TextView) view.findViewById(R.id.dialog_money_tv_content);
        tvToast = (TextView) view.findViewById(R.id.dialog_money_tv_toast);
        // 按钮系列
        tvBtnLeft = (TextView) view.findViewById(R.id.dialog_money_tv_left);
        tvBtnRight = (TextView) view.findViewById(R.id.dialog_money_tv_right);
        tvClose = (TextView) view.findViewById(R.id.dialog_money_tv_close);

    }

    private void initClick() {
        tvBtnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClick != null) {
                    if (ivDumpling != null) {
                        ivDumpling.setVisibility(View.INVISIBLE);
                    }
                    mClick.onBtnLeftClick();
                }
                if (isShowing()) {
                    dismiss();
                }
            }
        });
        tvBtnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClick != null) {
                    if (ivDumpling != null) {
                        ivDumpling.setVisibility(View.INVISIBLE);
                    }
                    mClick.onBtnRightClick();
                }
                if (isShowing()) {
                    dismiss();
                }
            }
        });
        tvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClick != null) {
                    if (ivDumpling != null) {
                        ivDumpling.setVisibility(View.GONE);
                    }
                    mClick.onCloseClick();
                }
                if (isShowing()) {
                    dismiss();
                }
            }
        });
        tvGreetingCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(strCoreField)) {

                    if (isShowing()) {
                        dismiss();
                    }
//                    Intent intent = new Intent();
//                    intent.setClass(mContext,)
                }
            }
        });
    }

    private void initData() {
        if (!TextUtils.isEmpty(strTitle)) {
            tvTitle.setText(Html.fromHtml("<font color = '#ffc21d' >" + strTitle + "</font>"));
            tvTitle.append(" 给你包了一个饺子");
        } else {
            tvTitle.setText("");
        }
        tvTitle.setVisibility(!TextUtils.isEmpty(strTitle) ? View.VISIBLE : View.GONE);

        tvContent.setText(!TextUtils.isEmpty(strContent) ? strContent : "");
        tvContent.setVisibility(!TextUtils.isEmpty(strContent) ? View.VISIBLE : View.GONE);

        tvToast.setText(!TextUtils.isEmpty(strToast) ? strToast : "");
        tvToast.setVisibility(!TextUtils.isEmpty(strToast) ? View.VISIBLE : View.GONE);

        tvBtnLeft.setText(!TextUtils.isEmpty(strBtnLeftText) ? strBtnLeftText : mContext.getString(R.string.dumpling_go_monery));
        tvBtnLeft.setVisibility(btnLeftVISIBLE);

        tvBtnRight.setText(!TextUtils.isEmpty(strBtnRightText) ? strBtnRightText : mContext.getString(R.string.dumpling_goon));
        tvBtnRight.setVisibility(btnRightVISIBLE);

        if (!TextUtils.isEmpty(strImgUrl)) {
            Picasso.with(mContext)
                    .load(strImgUrl)
                    .error(R.drawable.dumpling_default_avatar)
                    .transform(new Dumpling_PicassoCircleTransform())
                    .into(imgIcon);
            imgIcon.setVisibility(View.VISIBLE);
        } else {
            imgIcon.setImageResource(R.drawable.dumpling_default_avatar);
        }

    }

    // 根据不同的TYPE确定具体使用哪种类型
    private void initType() {
        if (TextUtils.isEmpty(strCoreField)) {
            return;
        }
        tvMoney.setVisibility(View.GONE);
        tvBlessing.setVisibility(View.GONE);
        tvGreetingCard.setVisibility(View.GONE);
        imgCouponSmall.setVisibility(View.GONE);
        imgCouponBig.setVisibility(View.GONE);
        if (mType == TYPE_MONEY) {              // 类型：钱
            tvMoney.setVisibility(!TextUtils.isEmpty(strCoreField) ? View.VISIBLE : View.GONE);
            tvMoney.setText(!TextUtils.isEmpty(strCoreField) ? strCoreField : "");
        } else if (mType == TYPE_BLESSING) {    // 类型：祝福
            tvBlessing.setVisibility(!TextUtils.isEmpty(strCoreField) ? View.VISIBLE : View.GONE);
            tvBlessing.setText(!TextUtils.isEmpty(strCoreField) ? strCoreField : "");
        } else if (mType == TYPE_GREETINGCARD) {
            tvGreetingCard.setVisibility(!TextUtils.isEmpty(strCoreField) ? View.VISIBLE : View.GONE);
        } else if (mType == TYPE_COUPON) {      // 类型：优惠券
            if (couponSize == COUPONSIZE_SMALL) {                // 优惠券：小
                imgCouponSmall.setVisibility(View.VISIBLE);
                Picasso.with(mContext)
                        .load(strCoreField)
                        .placeholder(R.drawable.dumpling_coupon_small)
                        .error(R.drawable.dumpling_coupon_small)
                        .into(imgCouponSmall);
            } else if (couponSize == COUPONSIZE_BIG) {             // 优惠券：大
                imgCouponBig.setVisibility(View.VISIBLE);
                Picasso.with(mContext)
                        .load(strCoreField)
                        .placeholder(R.drawable.dumpling_coupon_big)
                        .error(R.drawable.dumpling_coupon_big)
                        .into(imgCouponBig);
            }
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth(); //设置宽度
        lp.height = display.getHeight();
        getWindow().setAttributes(lp);
    }

    public static class Builder {

        private Context mContext;

        private String strImgUrl;

        private String strTitle;

        private String strCoreField;

        private String strContent;

        private String strToast;

        private String strBtnLeftText;

        private String strBtnRightText;

        private int btnLeftVISIBLE;

        private int btnRightVISIBLE;

        private GetMoneyOnSureClick mClick;

        private int mType;

        private ImageView imageView;

        private int couponSize;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }


        public Builder setImgUrl(String strImgUrl) {
            this.strImgUrl = strImgUrl;
            return this;
        }

        public Builder setTitle(String strTitle) {
            this.strTitle = strTitle;
            return this;
        }

        public Builder setCoreField(String strCoreField) {
            this.strCoreField = strCoreField;
            return this;
        }

        public Builder setContent(String strContent) {
            this.strContent = strContent;
            return this;
        }

        public Builder setToast(String strToast) {
            this.strToast = strToast;
            return this;
        }

        public Builder setBtnLeftVISIBLE(int VISIBLE) {
            this.btnLeftVISIBLE = VISIBLE;
            return this;
        }

        public Builder setBtnRightVISIBLE(int VISIBLE) {
            this.btnRightVISIBLE = VISIBLE;
            return this;
        }

        public Builder setBtnLeftText(String strBtnLeftText) {
            this.strBtnLeftText = strBtnLeftText;
            return this;
        }

        public Builder setBtnRightText(String strBtnRightText) {
            this.strBtnRightText = strBtnRightText;
            return this;
        }

        public Builder setOnClick(GetMoneyOnSureClick mClick) {
            this.mClick = mClick;
            return this;
        }

        public Builder setType(int mType) {
            this.mType = mType;
            return this;
        }

        public Builder setCouponSize(int couponSize) {
            this.couponSize = couponSize;
            return this;
        }

        public Builder setImageView(ImageView imageView) {
            this.imageView = imageView;
            return this;
        }

        public Dumpling_GetMoneyDialog builder() {
            return new Dumpling_GetMoneyDialog(mContext, this);
        }

    }

}
